package com.yixun.qfbao.util;

import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author zilong.liu
 * @date 2020-8-9
 *
 */
public class StringUtil {

	/**
	 * 
	 * @param seprateString   1,2,3,4,5 
	 * @param seprate		  ,
	 * @param returnType	  	
	 * @return					returnType = 1 则返回1，=2，则返回5 等于3，则返回1和5（小在前大在后）
	 */
	public static List<BigDecimal> getMinOrMaxFromSeprateString(String seprateString, String seprate, int returnType) {
		if(StringUtils.isBlank(seprateString)) {
			return null;
		}
		String[] spiltStrings = seprateString.split(seprate);
		if (spiltStrings == null || spiltStrings.length == 0) {
			return null;
		}
		List<BigDecimal> spiltInteger = new ArrayList<>();
		for (String splitString : spiltStrings) {
			if(StringUtils.isNotBlank(splitString)) {
				spiltInteger.add(new BigDecimal(BigDecimal.valueOf(
						Double.parseDouble(splitString)).stripTrailingZeros().toPlainString()));
			}
		}
		Collections.sort(spiltInteger);
		List<BigDecimal> returnList = new ArrayList<>();
		switch (returnType) {
		case 1:
			returnList.add(spiltInteger.get(0));
			break;
		case 2:
			returnList.add(spiltInteger.get(spiltInteger.size() - 1));
			break;
		case 3:
			returnList.add(spiltInteger.get(0));
			returnList.add(spiltInteger.get(spiltInteger.size() - 1));
			break;

		default:
			break;
		}
		return returnList;
	}
	
	/**
	 *  
	 * @param seprateString  1，2，3，4，5
	 * @param seprate			,
	 * @param appointNumber		3
	 * @return	1,2,3
	 */
	public static List<String> getAppointNumberListFromSeprateString(String seprateString, 
			String seprate, int appointNumber){
		List<String> seprateList = new ArrayList<>();
		if(StringUtils.isBlank(seprateString)) {
			return seprateList;
		}
		if (!(seprateString.indexOf(seprate) > -1)) {
			seprateList.add(seprateString);
			return seprateList;
		}
		String[] seprateStrings = seprateString.split(seprate);
		for (int i = 0, j = seprateStrings.length; i < j; i ++) {
			if(i < appointNumber)  seprateList.add(seprateStrings[i]);
			else break;
		}
		return seprateList;
	}
	
	/**
	 * 字符串分割成数组，并转换成list
	 * @param splitString
	 * @param split
	 * @return
	 */
	public static List<String> getListFromSplitString(String splitString, String split){
		if(StringUtils.isBlank(splitString)) {
			return null;
		}
		String[] splitStrings = splitString.split(split);
		return Arrays.asList(splitStrings);
	}
	
	/**
	 * 判断是否为 null "" "null" 
	 * @param strings
	 * @return
	 */
	public static boolean isNotNull(String ... strings) {
		for (String string : strings) {
			if(StringUtils.isEmpty(string) || "null".equals(string)) {
				return false;
			}
		}
		return true;
	}
	
	public static void main(String[] args) {
//		System.out.println(StringUtil.getMinFromSeprateStringByComma("1,3,5,4,2", ",", 3));
//		System.out.println(StringUtil.getAppointNumberListFromSeprateString("1,3,5,4,2", ",", 3));
		System.out.println(StringUtil.getListFromSplitString("12, 23, 34", ","));
	}
}
